<?php
namespace app\modules\member\models;

use Yii;
use app\models\Basic;
use common\component\tool\Tool;

class MemberCoupon extends Basic
{
    
    public static function tableName()
    {
        return '{{%member_coupon}}';
    }

    public function rules()
    {
        return [
            ['getbyappid', 'required', 'message' => 'getbyappid不能为空', 'on' => 'add'],
            ['getbyappid', 'string', 'message' => 'getbyappid必须是字符串',
                      'min' => 18, 'tooShort' => 'getbyappid最短18位',
                      'max' => 18, 'tooLong' => 'getbyappid最长18位'],
            // ['usebyappid', 'required', 'message' => 'usebyappid不能为空'],
            ['usebyappid', 'string', 'message' => 'usebyappid必须是字符串',
                      'min' => 18, 'tooShort' => 'usebyappid最短18位',
                      'max' => 18, 'tooLong' => 'usebyappid最长18位'],
            ['member_id', 'required', 'message' => 'member_id不能为空', 'on' => 'add'],
            ['member_id', 'integer', 'message' => 'member_id必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'member_id最小存储0', 
                        'max'=>4294967295, 'tooBig' => 'member_id最大存储4294967295'],
            ['cid', 'required', 'message' => 'cid不能为空', 'on' => 'add'],
            ['cid', 'integer', 'message' => 'cid必须为数字，或数字字符串', 
                        'min'=>0, 'tooSmall' => 'cid最小存储0', 
                        'max'=>4294967295, 'tooBig' => 'cid最大存储4294967295'],
            ['coupon_code', 'required', 'message' => 'coupon_code不能为空', 'on' => 'add'],
            ['coupon_code', 'string', 'message' => 'coupon_code必须是字符串',
                      'min' => 8, 'tooShort' => 'coupon_code最短8位',
                      'max' => 8, 'tooLong' => 'coupon_code最长8位'],
            ['couponname', 'required', 'message' => 'couponname不能为空', 'on' => 'add'],
            ['couponname', 'string', 'message' => 'couponname必须是字符串', 
                         'max' => 128, 'tooLong' => 'couponname最长128位'],
            ['flag', 'in', 'range' => ['1', '2'],  'message' => 'flag格式不正确'],
            ['condition_amount', 'integer', 'message' => 'condition_amount必须为数字，或数字字符串', 
                          'min'=>0, 'tooSmall' => 'condition_amount最小存储0', 
                          'max'=>65535, 'tooBig' => 'condition_amount最大存储65535'],
            ['amount', 'integer', 'message' => 'amount必须为数字，或数字字符串', 
                          'min'=>0, 'tooSmall' => 'amount最小存储0', 
                          'max'=>65535, 'tooBig' => 'amount最大存储65535'],
            ['discount', 'number', 'message' => 'discount格式不正确'],
            ['remark', 'string', 'message' => 'remark必须是字符串', 
                         'max' => 256, 'tooLong' => 'remark最长256位'],
            ['begin_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'begin_time非yyyy-MM-dd HH:mm:ss格式'],       
            ['end_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'end_time非yyyy-MM-dd HH:mm:ss格式'],
            ['isuse', 'in', 'range' => ['1', '2'],  'message' => 'isuse格式不正确'],
            ['use_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['use_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'use_time非yyyy-MM-dd HH:mm:ss格式'],
            ['useremark', 'string', 'message' => 'useremark必须是字符串', 
                         'max' => 256, 'tooLong' => 'useremark最长256位'],
            ['isdel', 'in', 'range' => ['1', '2'],  'message' => 'isdel格式不正确'],
            ['add_time', 'default', 'value' => '1000-01-01 00:00:00'],
            ['add_time', 'date', 'format'=>'yyyy-MM-dd HH:mm:ss', 'message' => 'add_time非yyyy-MM-dd HH:mm:ss格式'],
        ];
    }


    /*添加*/
    public function addCoupon($data)
    {
        $data['MemberCoupon']['coupon_code'] = $this->createCode();
        $data['MemberCoupon']['add_time'] = date('Y-m-d H:i:s', time());
        $this->scenario = 'add';
        // P($data);
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                // $id = $this->getPrimaryKey();
                return true;
            }
        }
        return false;
    }

    /*产生券码*/
    private function createCode()
    {
        return 'T' . date('md', time()) . Tool::generateRandomStr(3, 'num');
    }
    
}